package com.yl.applicationsecurity.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yl.applicationsecurity.mapper.InspectionConfigurationMapper;
import com.yl.applicationsecurity.mapper.InspectionMapper;
import com.yl.applicationsecurity.mapper.ScriptStateMapper;
import com.yl.applicationsecurity.pojo.Inspection;

import com.yl.applicationsecurity.pojo.InspectionConfiguration;
import com.yl.applicationsecurity.pojo.RadarVulnerability;
import com.yl.applicationsecurity.pojo.ScriptState;
import com.yl.applicationsecurity.service.InspectionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class InspectionServiceImpl extends ServiceImpl<InspectionMapper, Inspection> implements InspectionService {
    @Autowired
    private ScriptStateMapper scriptStateMapper;


    @Autowired
    private InspectionConfigurationMapper inspectionConfigurationMapper;

    @Override
    public PageInfo<Inspection> findListByInspection(String[] nationSelect,Inspection inspection, Date startCreateDate, Date endCreateDate ,Integer pageNum,Integer pageSize  ) {

        PageHelper.startPage(pageNum, pageSize );//每页显示8条

        LambdaQueryWrapper<Inspection> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if ( StringUtils.isNotEmpty(inspection.getUrl())){
            lambdaQueryWrapper.like(Inspection::getUrl,inspection.getUrl());
        }
        if ( StringUtils.isNotEmpty(inspection.getName())){
            lambdaQueryWrapper.like(Inspection::getName,inspection.getName());
        }
        if ( StringUtils.isNotEmpty(inspection.getSystem())){
            lambdaQueryWrapper.like(Inspection::getSystem,inspection.getSystem());
        }
        if ( StringUtils.isNotEmpty(inspection.getNation())){
            lambdaQueryWrapper.eq(Inspection::getNation,inspection.getNation());
        }
        if (null!=inspection.getStatus()){
            lambdaQueryWrapper.eq(Inspection::getStatus,inspection.getStatus());
        }

        if (null!=nationSelect&&nationSelect.length!=0){
            lambdaQueryWrapper.in(Inspection::getNation,nationSelect);
        }

        if(startCreateDate!=null){
            lambdaQueryWrapper.ge(Inspection::getCreateDate, startCreateDate);
        }


        if(endCreateDate!=null){
            lambdaQueryWrapper.le(Inspection::getCreateDate, endCreateDate);
        }

        lambdaQueryWrapper.orderByDesc(Inspection::getCreateDate);//根据首次发现时间排序
        List<Inspection> list = baseMapper.selectList(lambdaQueryWrapper);


        PageInfo<Inspection> pageInfo = new PageInfo<Inspection>(list);


        if (pageInfo.getPages() == 0) {
            pageInfo.setPages(1);
        }

        return pageInfo;
    }

    @Override
    public int updateStatus(String[]  ids,Integer status) {
        if(ids==null||ids.length==0){
            return 1;
        }
        if(status==99){//如果状态等于99，则代表已处理，并静默7天
            //首先查询出涉及到静默的告警信息
            LambdaQueryWrapper<Inspection> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(Inspection::getId , ids);
            List<Inspection> list= baseMapper.selectList(lambdaQueryWrapper);

            for (Inspection inspection : list){
                //对对应的配置数据设置告警，根据url  判断为是否是同一告警
               // LambdaQueryWrapper<InspectionConfiguration> inspectionConfigurationLambdaQueryWrapper = new LambdaQueryWrapper<>();
                LambdaUpdateWrapper<InspectionConfiguration> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
//                if ( StringUtils.isNotEmpty(inspection.getSystem())){
//                    lambdaUpdateWrapper.eq(InspectionConfiguration::getSystem,inspection.getSystem());
//                }
//
//                if ( StringUtils.isNotEmpty(inspection.getNation())){
//                    lambdaUpdateWrapper.eq(InspectionConfiguration::getNation,inspection.getNation());
//                }
//
//                if ( StringUtils.isNotEmpty(inspection.getName())){
//                    lambdaUpdateWrapper.eq(InspectionConfiguration::getName,inspection.getName());
//                }
                if ( StringUtils.isNotEmpty(inspection.getUrl())){
                    lambdaUpdateWrapper.eq(InspectionConfiguration::getUrl,inspection.getUrl());
                }
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DAY_OF_YEAR, 7);  // 当前日期加 7 天
                Date dateAfter7Days = calendar.getTime();  // 获取加 7 天后的日期
                lambdaUpdateWrapper.set(InspectionConfiguration::getEndSilenceTime,dateAfter7Days);
                inspectionConfigurationMapper.update(null,lambdaUpdateWrapper);
            }


            //设置为已处理
            LambdaUpdateWrapper<Inspection> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.set(Inspection::getStatus,1).in(Inspection::getId , ids);

            return baseMapper.update(null,lambdaUpdateWrapper);

        }



        LambdaUpdateWrapper<Inspection> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(Inspection::getStatus,status).in(Inspection::getId , ids);

        return baseMapper.update(null,lambdaUpdateWrapper);
    }

    @Override
    public int insert(Inspection inspection) {
        inspection.setId(UUID.randomUUID().toString());
        inspection.setStatus(0);//状态修改为待处理
        inspection.setCreateDate(new Date());//创建时间
        if(inspection.getUrl()!=null&inspection.getSystem()!=null&inspection.getNation()!=null&inspection.getRequtestText()!=null&inspection.getReponeseText()!=null){

            return baseMapper.insert(inspection);

        }
        return 0;
    }
}
